package com.ng.biz.controller;

import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.annotation.Tx;
import com.ng.common.controller.Controller;
import com.ng.common.model.response.R;
import com.ng.common.util.UuidUtil;
import com.ng.biz.model.domain.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

@Tag(name = "蓝图设计管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/blueprint/designer")
public class BlueprintDesignerController extends Controller {

    @Operation(summary = "获取蓝图设计列表")
    @Parameters({
        @Parameter(name = "keywords", description = "关键字", required = false),
        @Parameter(name = "cur", description = "当前页码", required = true),
        @Parameter(name = "limit", description = "每页数据量", required = true)
    })
    @TokenRequired
    @GetMapping("/pages")
    public R<Page<BlueprintDesigner>> pages(
            HttpServletRequest request,
            @RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
            @RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
            @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
        
        String sqlExceptSelect = " from " + BlueprintDesigner.TABLE_NAME + " a WHERE 1=1 ";
        if (!StrKit.isBlank(keywords)) {
            sqlExceptSelect += " and a.name like '%" + keywords + "%' ";
        }
        sqlExceptSelect += " ORDER BY a.update_time desc ";
        
        Page<BlueprintDesigner> page = BlueprintDesigner.dao.paginate(cur, limit, "select a.* ", sqlExceptSelect);
        return R.ok(page);
    }

    @Operation(summary = "保存蓝图设计信息")
    @Parameters({
        @Parameter(name = "blueprintDesigner", description = "蓝图设计信息", required = true)
    })
    @TokenRequired
    @Tx
    @PostMapping("/save")
    public R<BlueprintDesigner> save(HttpServletRequest request, @RequestBody BlueprintDesigner blueprintDesigner) {
        try {
            if (StrKit.notBlank(blueprintDesigner.getId())) {
            	blueprintDesigner.setUpdateUserId(getUserId(request));
            	blueprintDesigner.setUpdateTime(new Date(System.currentTimeMillis()));
                if (blueprintDesigner.update()) {
                    return R.ok(blueprintDesigner);
                } else {
                    return R.failed("更新失败！");
                }
            } else {
            	blueprintDesigner.setId(UuidUtil.getUUID());
            	blueprintDesigner.setCreateUserId(getUserId(request));
            	blueprintDesigner.setCreateTime(new Date(System.currentTimeMillis()));
                if (blueprintDesigner.save()) {
                    return R.ok(blueprintDesigner);
                } else {
                    return R.failed("保存失败.");
                }
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    @Operation(summary = "删除蓝图设计信息")
    @Parameters({
        @Parameter(name = "ids", description = "蓝图设计id集合(逗号分割)", required = true)
    })
    @DeleteMapping("/{ids}")
    @TokenRequired
    public R<String> delete(@PathVariable("ids") String ids) {
        String[] arr = ids.split(",");
        if (ids == null || ids.length() <= 0) {
            return R.failed("请选择需要删除的蓝图设计信息.");
        }
        for (String id : arr) {
        	BlueprintDesigner.dao.deleteById(id);
        	BlueprintDesignerContent.dao.deleteById(id);
        }
        return R.ok();
    }
    
    @Operation(summary = "保存蓝图设计内容信息")
    @Parameters({
        @Parameter(name = "blueprintDesignerContent", description = "蓝图设计内容信息", required = true)
    })
    @TokenRequired
    @Tx
    @PostMapping("/save/content")
    public R<String> saveBlueprintDesignerContent(HttpServletRequest request, @RequestBody BlueprintDesignerContent blueprintDesignerContent) {
        try {
        	BlueprintDesignerContent oldBlueprintDesignerContent=BlueprintDesignerContent.dao.findById(blueprintDesignerContent.getId());
            if (oldBlueprintDesignerContent!=null) {
            	blueprintDesignerContent.setUpdateUserId(getUserId(request));
            	blueprintDesignerContent.setUpdateTime(new Date(System.currentTimeMillis()));
                if (blueprintDesignerContent.update()) {
                    return R.ok();
                } else {
                    return R.failed("更新失败！");
                }
            } else {
            	blueprintDesignerContent.setCreateUserId(getUserId(request));
            	blueprintDesignerContent.setCreateTime(new Date(System.currentTimeMillis()));
                if (blueprintDesignerContent.save()) {
                    return R.ok();
                } else {
                    return R.failed("保存失败.");
                }
            }
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "蓝图设计内容信息")
    @Parameters({
        @Parameter(name = "id", description = "蓝图设计id", required = true)
    })
    @GetMapping("/content/{id}")
    @TokenRequired
    public R<BlueprintDesignerContent> getContentById(@PathVariable("id") String id) {
    	return R.ok(BlueprintDesignerContent.dao.findById(id));
    }

}
